package com.xique.system.controller;

import com.xique.common.core.constant.Constants;
import com.xique.common.core.domain.R;
import com.xique.common.core.utils.ServletUtils;
import com.xique.common.core.utils.ip.IpUtils;
import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.system.bean.domain.SystemLoginInfo;
import com.xique.system.service.biz.service.ISystemLogBizService;
import com.xique.system.service.service.ISystemLoginInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author shd
 * @create 2021/11/12 9:17
 */
@RestController
@RequestMapping("/log")
public class SystemLogController extends BaseController {

    @Autowired
    private ISystemLogBizService systemLogBizService;

    @Autowired
    private ISystemLoginInfoService loginInfoService;

    @PostMapping("/saveLoginInfo")
    @ApiOperation("存储登录日志")
    public R<Boolean> saveLoginInfo(@RequestParam("username") String username,
                                    @RequestParam("status") String status,
                                    @RequestParam("message") String message,
                                    @RequestParam("platform") String platform) {
        return systemLogBizService.saveLoginInfo(username, status, message, platform);
    }

    @PreAuthorize(hasPermi = "system:logininfor:list")
    @GetMapping("/list")
    public TableDataInfo list(SystemLoginInfo logininfor)
    {
        startPage();
        List<SystemLoginInfo> list = loginInfoService.selectLogininforList(logininfor);
        return getDataTable(list);
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize(hasPermi = "system:logininfor:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SystemLoginInfo logininfor) throws IOException
    {
        List<SystemLoginInfo> list = loginInfoService.selectLogininforList(logininfor);
        ExcelUtil<SystemLoginInfo> util = new ExcelUtil<SystemLoginInfo>(SystemLoginInfo.class);
        util.exportExcel(response, list, "登录日志");
    }

    @PreAuthorize(hasPermi = "system:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable Long[] infoIds)
    {
        return toAjax(loginInfoService.deleteLogininforByIds(infoIds));
    }

    @PreAuthorize(hasPermi = "system:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/clean")
    public AjaxResult clean()
    {
        loginInfoService.cleanLogininfor();
        return AjaxResult.success();
    }
}
